package com.martinwj.controller.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.martinwj.entity.Field;
import com.martinwj.entity.Result;
import com.martinwj.entity.Video;
import com.martinwj.exception.SysException;
import com.martinwj.service.FieldService;
import com.martinwj.service.MediaService;
import com.martinwj.service.SlideProfileService;
import com.martinwj.service.VideoService;
import it.sauronsoftware.jave.VideoInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ApiAction
 * @Description: TODO
 * @author: martin-wj
 * @createDate: 2020-12-22
 */
@Controller
@RequestMapping("api")
public class ApiAction {

    @Autowired
    private SlideProfileService slideProfileService;
    @Autowired
    private MediaService mediaService;
    @Autowired
    private VideoService videoService;
    @Autowired
    private FieldService fieldService;

    /**
     * 幻灯片数据
     * @param apiId 接口表主键
     * @return
     */
    @RequestMapping("slide.json")
    @ResponseBody
    public Result slide(
            HttpServletRequest request,
            @RequestParam(value="apiId") String apiId) {

        List<Map<String, Object>> list = slideProfileService.mapListByApiId(apiId);

        return Result.success().add("list", list);
    }

    /**
     * 获取该视频的信息
     * @param videoId 视频主键
     * @return
     */
    @RequestMapping("get_video_info.json")
    @ResponseBody
    public Result getVideoInfo(
            HttpServletRequest request,
            @RequestParam(value="videoId") String videoId) {

        Video video = videoService.selectById(videoId);

        return Result.success().add("videoInfo", video);
    }

    /**
     * 获取视频地址
     * @param videoId 視頻信息的主键
     * @return
     */
    @RequestMapping("get_video_play.json")
    @ResponseBody
    public Result getVideoPlay(
            @RequestParam(value="videoId") String videoId) {

        String videoPlay = videoService.selectVideoPlayById(videoId);

        return Result.success().add("videoPlay", videoPlay);
    }

    /**
     * 获取该视频的播放列表
     * @param mediaId 媒体信息的主键
     * @return
     */
    @RequestMapping("get_video_list.json")
    @ResponseBody
    public Result getVideoList(
            @RequestParam(value="mediaId") String mediaId) {

        List<Video> list = videoService.listByMediaId(mediaId);

        return Result.success().add("list", list);
    }

    /**
     * 根据主键，获取媒体信息
     * @param mediaId 媒体信息的主键
     * @return
     * @throws SysException
     */
    @RequestMapping("get_media_info.json")
    @ResponseBody
    public Result getMediaInfo(
            @RequestParam(value="mediaId") String mediaId) throws SysException {

        Map<String, Object> info = mediaService.selectByMediaId(mediaId);

        return Result.success().add("info", info);
    }

    /**
     * 搜索
     * @param keyWord 关键词
     * @return
     * @throws SysException
     */
    @RequestMapping("search.json")
    @ResponseBody
    public Result search(
            @RequestParam(value="keyWord") String keyWord,
            @RequestParam(value="pageNum", defaultValue="1") int pageNum,
            @RequestParam(value="pageSize", defaultValue="10") int pageSize) throws SysException {

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        // pageHelper分页插件
        // 只需要在查询之前调用，传入当前页码，以及每一页显示多少条
        PageHelper.startPage(pageNum, pageSize);
        List<String> mediaIdList = mediaService.search(keyWord);
        PageInfo<String> pageInfo = new PageInfo<String>(mediaIdList);

        if (mediaIdList!=null && !mediaIdList.isEmpty()) {
            int len = mediaIdList.size();
            for (int i=0; i<len; i++) {
                String mediaId = mediaIdList.get(i);
                // 2.1 获取每一条媒体信息的主要数据
                Map<String, Object> mediaMap = mediaService.selectByMediaId(mediaId);

                // 2.2 获取每一条媒体信息下的集数
                List<Video> videoList = videoService.listByMediaId(mediaId);
                mediaMap.put("videoList", videoList);

                list.add(mediaMap);
            }
        }

        return Result.success()
                .add("pageInfo", pageInfo)
                .add("list", list)
                ;
    }

    /**
     * 获取分类筛选字段的信息
     * @param typeId 分类信息的主键
     * @return
     */
    @RequestMapping("get_list_field.json")
    @ResponseBody
    public Result getListField(
            @RequestParam(value="typeId") String typeId) {

        List<Field> list = fieldService.getListField(typeId);

        return Result.success().add("list", list);
    }

    /**
     * 根据筛选结果，查询媒体列表
     * @param filterArr 筛选结果数组
     * @return
     */
    @RequestMapping("get_media_list_by_filter.json")
    @ResponseBody
    public Result getMediaLisByFilter(
            @RequestParam(value="typeId") String typeId,
            @RequestParam(required=false, value="filterArr") String[] filterArr,
            @RequestParam(value="pageNum", defaultValue="1") int pageNum,
            @RequestParam(value="pageSize", defaultValue="42") int pageSize) {

//        for (int i = 0; i< filterArr.length; i++) {
//            String str = filterArr[i];
//            filterArr[i] = str.substring(0, str.indexOf("="));
//            System.out.println("filterArr: " + filterArr[i]);
//        }

        // 获取分类筛选字段的信息
        List<Field> fieldList = fieldService.getListField(typeId);

        Map<String, Object> param = new HashMap<String, Object>();
        for (int i=0; i<fieldList.size(); i++) {
            if (!"0".equals(filterArr[i].split("=")[1])) {
                param.put(fieldList.get(i).getVarName(), filterArr[i].split("=")[1]);
            }
        }

        // 查询该分类下的所有可筛选字段
        List<Field> fieldInfoList = fieldService.listCategoryField(typeId);

        // pageHelper分页插件
        // 只需要在查询之前调用，传入当前页码，以及每一页显示多少条
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> list = mediaService.listByFilter(typeId, param, fieldInfoList);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(list);

        for (Map<String, Object> m : list) {
            for (String s : m.keySet()) {
                System.out.println(s + " ====> " + m.get(s));
            }
            System.out.println();
        }

        return Result.success().add("pageInfo", pageInfo);
    }


    /**
     * 根据接口自定义查询数据
     * @return
     * @throws SysException
     */
    @RequestMapping("data.json")
    @ResponseBody
    public Result data(
            @RequestParam(value="apiId") String apiId) throws SysException {

        List<Map<String, Object>> list = mediaService.getDataByApiId(apiId);

        return Result.success().add("list", list);
    }

    /**
     * 根据接口自定义查询排行榜数据
     * @return
     * @throws SysException
     */
    @RequestMapping("rank.json")
    @ResponseBody
    public Result rank(
            @RequestParam(value="apiId") String apiId) throws SysException {

        List<Map<String, Object>> list = mediaService.getRankDataByApiId(apiId);

        return Result.success().add("list", list);
    }

    /**
     * 获取最新视频
     * @return
     * @throws SysException
     */
    @RequestMapping("get_new_video_list.json")
    @ResponseBody
    public Result getNewVideoList(
            @RequestParam(value="num") String num) throws SysException {

        List<Video> list = videoService.listNewVideo(num);

        return Result.success().add("list", list);
    }
}
